const app = getApp()

import axios from '../../utils/axios'

Page({

  data: {
    firstIndex: 0, // 当前选中的一级分类
    firstTypeList: [], // 一级分类list
    nextTypeList: [], // 二级分类list
  },

  onLoad: function (options) {
    this.selectFirstType()
  },

  onShow: function () {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 1
      })
    }

    if (app.globalData.checkNav == "brand" && this.data.firstTypeList.length != 0) {
      app.globalData.checkNav = ''
      this.setData({
        firstIndex: this.data.firstTypeList.length - 1
      })
      this.selectBrandList()
    }

  },

  // 点击新品排行
  checkNew: function () {
    wx.navigateTo({
      url: '/pages/commodity/commodity?newSort=1',
    })
  },

  // 点击热销排行
  checkHot: function () {
    wx.navigateTo({
      url: '/pages/commodity/commodity?saleSort=1',
    })
  },

  // 点击二级分类跳转到商品页面
  skipCommodity: function (e) {

    if (e.currentTarget.dataset.typename == "品牌屋") {
      wx.navigateTo({
        url: '/pages/commodity/commodity?brandId=' + e.currentTarget.dataset.typeid + "&typeName=品牌屋",
      })
    } else {
      wx.navigateTo({
        url: '/pages/commodity/commodity?typeId=' + e.currentTarget.dataset.typeid  + "&typeName=" + e.currentTarget.dataset.typename,
      })
    }

  },

  // 点击一级分类
  cutFirstType: function (e) {
    this.setData({
      firstIndex: e.currentTarget.dataset.index
    })
    if (this.data.firstIndex == this.data.firstTypeList.length - 1) {
      this.selectBrandList()
    } else {
      this.selectNextType()
    }

  },

  // 查询品牌列表
  selectBrandList: function () {

    axios('/cloud/goodsBrand', {
      store_id: app.globalData.storeId
    }).then(res => {
      console.log(res)
      if (res.data.resultcode == 1) {
        let list = res.data.result
        for (let i = 0; i < list.length; i++) {
          list[i]['name'] = list[i].brand_name
          list[i]['type_id'] = list[i].brand_id
          list[i]['type'] = 'brand'
        }
        this.setData({
          nextTypeList: list
        })
      }
    })
  },

  // 查询二级分类
  selectNextType: function () {

    let pid = this.data.firstTypeList[this.data.firstIndex].type_id

    axios('/cloud/goodsType', {
      store_id: app.globalData.storeId,
      pid: pid
    }).then(res => {
      console.log(res)
      if (res.data.resultcode == 1) {
        this.setData({
          nextTypeList: res.data.result
        })
      }
    })
  },

  // 查询第一级分类
  selectFirstType: function () {
    axios('/cloud/goodsType', {
      store_id: app.globalData.storeId
    }).then(res => {
      console.log(res)
      if (res.data.resultcode == 1) {
        let item = {
          name: "品牌屋",
          type_id: "brand"
        }
        res.data.result.push(item)
        this.setData({
          firstTypeList: res.data.result
        })
        if (app.globalData.checkNav == "brand") {
          app.globalData.checkNav = ''
          this.setData({
            firstIndex: res.data.result.length - 1
          })
          this.selectBrandList()
        } else {
          this.selectNextType()
        }

      }
    })
  },

  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})